﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Drawing.Drawing2D;

namespace Djs.Common.Components
{
    /// <summary>
    /// Abstract ancestor for interactive item, which contain collection of IInteractiveItem
    /// </summary>
    public abstract class InteractiveContainer : InteractiveItem, IInteractiveContainer, IInteractiveItem
    {
        #region Items
        /// <summary>
        /// Interactive items in this container.
        /// Any collection can be stored.
        /// Set of value does not trigger Draw().
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public IEnumerable<IInteractiveItem> Items
        {
            get { return this.ItemList; }
            set { this._ItemList = new List<IInteractiveItem>(value); }
        }
        /// <summary>
        /// Add one interactive item.
        /// Does not trigger Draw().
        /// </summary>
        /// <param name="item"></param>
        public void AddItem(IInteractiveItem item)
        {
            this.ItemList.Add(item);
        }
        /// <summary>
        /// Add more interactive items.
        /// Does not trigger Draw().
        /// </summary>
        /// <param name="item"></param>
        public void AddItems(params IInteractiveItem[] items)
        {
            this.ItemList.AddRange(items);
        }
        /// <summary>
        /// Add more interactive items.
        /// Does not trigger Draw().
        /// </summary>
        /// <param name="item"></param>
        public void AddItems(IEnumerable<IInteractiveItem> items)
        {
            this.ItemList.AddRange(items);
        }
        protected List<IInteractiveItem> ItemList
        {
            get
            {
                if (this._ItemList == null)
                    this._ItemList = new List<IInteractiveItem>();
                return this._ItemList;
            }
        }
        private List<IInteractiveItem> _ItemList;
        /// <summary>
        /// Bounds for inner items.
        /// In this cordinates are active and drawed item from this.ItemList.
        /// This coordinates are relative to this.ActiveBounds.
        /// Thus: ActiveBounds are coordinates in this parent; 
        /// in this.ActiveBounds are this.ItemBounds; an in ItemBounds are this.ItemList.
        /// When have InteractiveControl with ClientRectangle = {0,0,1000,600}, 
        /// in this control is an InteractiveContainer on coordinates ActiveBounds = {100, 50, 300, 200},
        /// that container have ItemBounds = { 10, 10, 280, 180 } = in center of container (with border 10 px),
        /// and in that container is in ItemList an item with ActiveBounds = { 60, 40, 20, 20 }, 
        /// then this item are on coordinates: 
        /// X = (InteractiveControl.ClientRectangle.X + InteractiveContainer.ActiveBounds.X + InteractiveContainer.ItemBounds.X + item.ActiveBounds.X = 0 + 100 + 10 + 60 = 170px
        /// </summary>
        public virtual Rectangle ItemBounds { get { return new Rectangle(new Point(0,0), this.ActiveBounds.Size); } }

        List<IInteractiveItem> IInteractiveContainer.ItemList { get { return this._ItemList; } }
        Rectangle IInteractiveContainer.ItemBounds { get { return this.ItemBounds; } }
        #endregion
        #region Interactive property and methods
        /// <summary>
        /// Repaint all items (not just me) after current events.
        /// </summary>
        protected override bool RepaintAllItems
        {
            get
            {
                if (base.RepaintAllItems) return true;
                return this.ItemList.Any(i => i.RepaintAllItems);
            }
            set
            {
                base.RepaintAllItems = value;
                foreach (IInteractiveItem item in this.ItemList)
                    item.RepaintAllItems = value;
            }
        }
        #endregion
        #region Draw to Graphic
        /// <summary>
        /// InteractiveContainer.Draw(): call PaintBackground
        /// </summary>
        /// <param name="e"></param>
        protected override void Draw(GInteractiveDrawArgs e)
        {
            this.PaintBackground(e);
        }
        protected virtual void PaintBackground(GInteractiveDrawArgs e)
        {
        }
        #endregion
    }
}
